package com.rzgeo.wcgallay;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.rzgeo.rzmaplib.util.RmlUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class WcmReseMgrActv extends AppCompatActivity {

    /**
     * token : {"AccessKeySecret":"","AccessKeyId":"","Expiration":"","SecurityToken":""}
     */

    private TokenBean token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rese_mgr_actv);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public TokenBean getToken() {
        return token;
    }

    public void setToken(TokenBean token) {
        this.token = token;
    }

    public static class TokenBean {
        /**
         * AccessKeySecret :
         * AccessKeyId :
         * Expiration :
         * SecurityToken :
         */

        private String AccessKeySecret;
        private String AccessKeyId;
        private String Expiration;
        private String SecurityToken;

        public String getAccessKeySecret() {
            return AccessKeySecret;
        }

        public void setAccessKeySecret(String AccessKeySecret) {
            this.AccessKeySecret = AccessKeySecret;
        }

        public String getAccessKeyId() {
            return AccessKeyId;
        }

        public void setAccessKeyId(String AccessKeyId) {
            this.AccessKeyId = AccessKeyId;
        }

        public String getExpiration() {
            return Expiration;
        }

        public void setExpiration(String Expiration) {
            this.Expiration = Expiration;
        }

        public String getSecurityToken() {
            return SecurityToken;
        }

        public void setSecurityToken(String SecurityToken) {
            this.SecurityToken = SecurityToken;
        }
    }

    public class OSSAct extends AsyncTask<Void,Void,TokenBean> {

        @Override
        protected TokenBean doInBackground(Void... params) {
            return queryToken();
        }

        @Override
        protected void onPostExecute(TokenBean tokenBean) {
            super.onPostExecute(tokenBean);

        }

        protected TokenBean queryToken(){

            String path = getString(R.string.url_osskey);
            try {
                URL url = new URL(path);

                //至少要设置的两个请求头
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                //获得结果码
//                int responseCode = connection.getResponseCode();
                int responseCode= 200;
//                connection.setRequestProperty("Content-Type", "application/json");
//                作者：AlicFeng
//                链接：https://www.jianshu.com/p/ba6998e0dfa7
//                來源：简书
//                著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
                if (responseCode == 200) {
                    //请求成功
                    InputStream is = connection.getInputStream();
                    String json = RmlUtil.Convert.inputStream2String(is);
                    return JSON.parseObject(json,TokenBean.class);
                } else {
                    //请求失败
                    return null;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
